package vn.edu.hut.soict.lsom.align;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.net.URI;
import java.net.URISyntaxException;

import org.semanticweb.owl.align.Alignment;
import org.semanticweb.owl.align.AlignmentException;
import org.semanticweb.owl.align.AlignmentVisitor;
import org.semanticweb.owl.align.Evaluator;


import vn.edu.hut.soict.lsom.utils.DCIn;

import fr.inrialpes.exmo.align.impl.BasicParameters;
import fr.inrialpes.exmo.align.impl.eval.PRecEvaluator;
import fr.inrialpes.exmo.align.impl.renderer.RDFRendererVisitor;
import fr.inrialpes.exmo.align.parser.AlignmentParser;

public class AlignTest {

	/**
	 * @param args
	 * @throws AlignmentException 
	 * @throws IOException 
	 * @throws URISyntaxException 
	 */
	public static void main(String[] args) throws AlignmentException, IOException, URISyntaxException {
		// TODO Auto-generated method stub
		Properties p = new BasicParameters();
		AlignmentParser aparser = new AlignmentParser(0); 
		Alignment reference = aparser.parse( new File( "./benchmark/reference_2011.rdf" ).toURI() );
		Alignment al = aparser.parse( new File( "./benchmark/reference_2011.rdf" ).toURI() );
		
		Properties param = new BasicParameters();
		Map<String,String> human2UMLS = new HashMap<String, String>();
		Map<String,String> UMLS2human = new HashMap<String, String>();
		DCIn.simpleRead(human2UMLS, UMLS2human, "./data/human_2_umls");
		param.put("human2UMLS", human2UMLS);
		param.put("UMLS2human", UMLS2human);
		Map<String,String> mouse2UMLS = new HashMap<String, String>();
		Map<String,String> UMLS2mouse = new HashMap<String, String>();
		DCIn.simpleRead(mouse2UMLS, UMLS2mouse, "./data/mouse_2_umls");
		param.put("mouse2UMLS", mouse2UMLS);
		param.put("UMLS2mouse", UMLS2mouse);
		
		File f2 = new File("./benchmark/human.owl");
		File f1 = new File("./benchmark/mouse.owl");

		URI u1 = f1.toURI();
		URI u2 = f2.toURI();
		CMAlignment myAlign = new CMAlignment();
		myAlign.init( u1, u2 );
		myAlign.align((Alignment) null, param);
		
		Evaluator evaluator = new PRecEvaluator(reference,myAlign); 
		evaluator.eval(p);
		
//		PrintWriter writer = new PrintWriter ( new BufferedWriter( new OutputStreamWriter( System.out, "UTF-8" )), true); 
//		AlignmentVisitor renderer = new RDFRendererVisitor(writer); 
//		myAlign.render(renderer); 
//		writer.flush(); 
//		writer.close();
		System.out.println("Fmeasure : "+((PRecEvaluator)evaluator).getFmeasure()+" over "+myAlign.nbCells()+" cells"); 
		System.out.println("Precision : "+((PRecEvaluator)evaluator).getPrecision()+" over "+myAlign.nbCells()+" cells"); 
		System.out.println("Recall : "+((PRecEvaluator)evaluator).getRecall()+" over "+myAlign.nbCells()+" cells"); 
		
		compose(mouse2UMLS, UMLS2human);


	}
	
	public static Map<String,String> compose(Map<String,String> map12, Map<String,String> map23){
		long start = System.currentTimeMillis();
		Map<String,String> map13 = new HashMap<String, String>();
		Iterator<Entry<String,String>> it = map12.entrySet().iterator();
		while(it.hasNext()){
			Entry<String,String> entry = it.next();
			String c2 = entry.getValue();
			if (map23.containsKey(c2)){
				map13.put(entry.getKey(), map23.get(c2));
			}
		}
		long end = System.currentTimeMillis();
		System.out.println();
	    System.out.println("Compose time: " + (end-start) +"ms");
	    System.out.println("Size: " + map13.size());
	    return map13;
	}

}
